#include <ResColor.h>
#include <kernel/tinyxml2/tinyxml2.h>
#include <log/log_err.h>
#include <value/TinyXml.h>

using namespace tinyxml2;

static const char* filename = "res/value/colors.xml";

ResColor::ResColor(/* args */)
{

}

ResColor::~ResColor()
{

}

int ResColor::getColor(const char* name)
{
    int rescolor = 0;
    XMLDocument *doc = new XMLDocument();
    XMLElement* resources = nullptr;
    XMLElement* colors = nullptr;
    XMLError errorId = XML_SUCCESS;
    const char *buffer = "failed";
    
    doc->LoadFile(filename);
    errorId = doc->ErrorID();
    SIGNWAY_ERROR_GOTO(errorId > 0, RETURN, "file %s load failed", filename);

    resources = doc->FirstChildElement("resources");
    SIGNWAY_ERROR_GOTO(resources == nullptr, RETURN, "not find resouces");

    colors = TinyXml::findAttribute(resources, "color", "name", name);
    SIGNWAY_ERROR_GOTO(colors == nullptr, RETURN, 
                        "not find colors or colors attr is %s", name);
    rescolor = colors->IntText(0);
    SIGNWAY_LOGI("%#X color", rescolor);

RETURN:
    if (doc != nullptr) {
        delete doc; doc = nullptr;
    }
    return rescolor;
}